package com.micro.mq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 批量消费消息demo
 *
 * @author pan.shun
 * @since 2022/7/19 17:03
 */
@Slf4j
@Component
public class KafkaBatchConsumer {

    /**
     * 批量消费数据
     * <p>
     * 在 配置文件中设置【max-poll-records】 最大消费次数，
     * 同时将 【 kafka.listener.type 】中改成 batch
     */
    @KafkaListener(topics = "${app.topic.foo2}", groupId = "${spring.kafka.consumer.group-id}")
    public void receive2(List<ConsumerRecord<?, ?>> cr, Acknowledgment ack) {
        int size = cr.size();
        log.info("初始数据长度为：{}", size);
        ack.acknowledge();
    }
}
